#!/usr/bin/env bash
# author: lanwenping
# date: 2020-05-18
# 检查 http 服务是否正常

echo "start "
function getHttpCodeFun(){
    local URL=http://www.baidu.com
    # 尝试次数,时间间隔
    local TRY_TIMES=10
    # sleep 3 默认秒  3s 3m 3h 秒 分 小时
    local INTERVAL_SECONDS=30
    local CUR_TIMES=0
    while (( $times<${TRY_TIMES} ))
#    while [[ $times -lt ${TRY_TIMES} ]]
    do
        # sleep ${INTERVAL_SECONDS}
        CUR_TIMES=$[CUR_TIMES+1]
        HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`
        # return $HTTP_CODE;
        # 000 为非正常码
        if [ $HTTP_CODE = 200 ]
        then
         echo "正常访问应用: url: ${URL}"
         return 0
        fi
    done
    echo "无法正常访问应用, url: ${URL}"
    echo "访问次数: ${CUR_TIMES}"
    return 1
}
getHttpCodeFun
echo $?


